/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-23
*/
package com.rzico.article.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.account.entity.Payment;
import com.rzico.annotation.Log;
import com.rzico.article.entity.Article;
import com.rzico.article.entity.EnrollOrder;
import com.rzico.article.service.ArticleService;
import com.rzico.article.service.EnrollOrderService;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报名记录控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-23
 */
@Api(description = "报名记录接口")
@RestController
@RequestMapping("/member/enrollOrder")
public class EnrollOrderMemberController extends BaseController {

    @Autowired
    private EnrollOrderService enrollOrderService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SysSequenceService sysSequenceService;

    /**
     * 分页查询报名记录
     *
     * @return
    */
    @ApiOperation("分页查询报名记录")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "title", value = "标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "状态 {0:待付款,1:已付款,2:已完成,3:已取消}", dataType = "Integer", paramType = "query")
    })
    public CommResult<EnrollOrder> list(String title,Integer orderStatus, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        Member member = memberService.getCurrent();
        if (member!=null) {
            params.put("memberId", member.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        params.put("deleted", false);

        if (title!=null) {
            params.put("title", title);
        }

        if (orderStatus!=null) {
            params.put("orderStatus", orderStatus);
        }
        params.put("paymentStatus", 1);

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<EnrollOrder> list = enrollOrderService.selectList(params);
        PageResult<EnrollOrder> pageResult = new PageResult<EnrollOrder>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条报名记录
     *
     * @return
    */
    @ApiOperation("查询单条报名记录")
    @GetMapping("/find/{id}")
    public CommResult<EnrollOrder> find(@PathVariable String id) {
        EnrollOrder result = enrollOrderService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("enrollOrder",result);
        return CommResult.success(data);

    }

    /**
     * 判断是否购买
     *
     * @return
     */
    @ApiOperation("判断是否购买")
    @GetMapping("/checkExists")
    public CommResult<EnrollOrder> checkExists(Long articleId) {
        Article article = articleService.findById(articleId);
        Member member = memberService.getCurrent();
        if (member==null) {
            return CommResult.error("没有注册会员");
        }
        EnrollOrder enrollOrder = enrollOrderService.checkExists(article,member);
        Map<String,Object> data = new HashMap<>();
        data.put("enrollOrder",enrollOrder);
        return CommResult.success(data);

    }

    /**
     *   创建测评订单
     *
     * @return
     */
    @ApiOperation("创建测评订单")
    @PostMapping("/create")
    public CommResult<EnrollOrder> create(Long articleId,String phone,String contact,String userId) {

        Article article = articleService.findById(articleId);
        if (article==null) {
            return CommResult.error("无效报名id");
        }

        Member member = memberService.getCurrent();
        if (member==null) {
            return CommResult.error("没有注册会员");
        }

        String sn = sysSequenceService.generate("order");
        EnrollOrder enrollOrder = enrollOrderService.create(article,member,sn);
        enrollOrder.setContact(contact);
        enrollOrder.setPhone(phone);
        enrollOrder.setUserId(userId);
        enrollOrderService.updateByPrimaryKeySelective(enrollOrder);

        Map<String,Object> data = new HashMap<>();
        data.put("enrollOrder",enrollOrder);
        if (enrollOrder.getPaymentStatus().equals(0)) {
            String psn = sysSequenceService.generate("payment");
            Payment payment = enrollOrderService.payment(enrollOrder,psn);
            data.put("payment",payment);
        }
        return CommResult.success(data);

    }

    /**
     * @return
     */
    @ApiOperation("我推广的报名")
    @GetMapping("/enrollOrderList")
    public CommResult<EnrollOrder> enrollOrderList(Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        Member member = memberService.getCurrent();
        params.put("promoter1", member.getId());

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<EnrollOrder> list = enrollOrderService.selectList(params);
        PageResult<EnrollOrder> pageResult = new PageResult<EnrollOrder>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

}
